package com.goldcn.common;

import com.goldcn.common.util.UuidUtils;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;


public class CommonUtils {
	public static Logger logger = Logger.getLogger(CommonUtils.class);

	public static String getGUID() {
		return UuidUtils.uuid();
	}

	/**
	 * MD5加密
	 * @param data 任意字符串
	 * @return 加密后的字符串
	 * @author kylin.woo
	 * @version 1.0
	 */
	public synchronized static final String MD5(String data){
		return hash(sha(hash(data)));
	}


	public static boolean isEmpty(String str) {
		boolean temp = true;
		if (null != str && !"".equals(str) && !"".equals(str.trim())
				&& !"null".equals(str.trim())) {
			temp = false;
		}
		return temp;
	}

	public static boolean isEmpty(String[] str) {
		boolean temp = true;
		for (String s : str) {
			temp = isEmpty(s);
			if (temp) {
				break;
			} else {
				continue;
			}
		}
		return temp;
	}

	public static boolean isNotEmpty(String str) {
		boolean temp = false;
		if (null != str && !"".equals(str) && !"".equals(str.trim())
				&& !"null".equals(str.trim())) {
			temp = true;
		}
		return temp;
	}

	public static String trim(String str) {
		String trimStr = "";
		if (null != str && !"".equals(str)) {
			trimStr = str.trim();
		}
		return trimStr;
	}

	public static boolean equals(String str, String str2) {
		if (isEmpty(str2) || isEmpty(str)) {
			return false;
		}
		return str.equals(str2);
	}

	private static MessageDigest digest = null;

	public synchronized static final String hash(String data) {
		if (digest == null) {
			try {
				digest = MessageDigest.getInstance("MD5");
			} catch (NoSuchAlgorithmException nsae) {

			}
		}
		// Now, compute hash.
		try {
			digest.update(data.getBytes("utf-8"));
		} catch (UnsupportedEncodingException e) {

		}
		return encodeHex(digest.digest());
	}

	/**
	 * SHA加密
	 * @param data 任意字符串
	 * @return 加密后的字符串
	 * @author kylin.woo
	 * @version 1.0
	 */
	public synchronized static final String sha(String data){
		MessageDigest md5;
		try {
			md5 = MessageDigest.getInstance("SHA");
			md5.update(data.getBytes("utf-8"));
			return encodeHex(md5.digest());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static final String encodeHex(byte[] bytes) {
		StringBuffer buf = new StringBuffer(bytes.length * 2);
		int i;

		for (i = 0; i < bytes.length; i++) {
			if (((int) bytes[i] & 0xff) < 0x10) {
				buf.append("0");
			}
			buf.append(Long.toString((int) bytes[i] & 0xff, 16));
		}
		return buf.toString();
	}


	public static Map<String,String> getParameterMap(HttpServletRequest request) {
		Map<String,String> param = new HashMap<String,String>();
		StringBuilder sb = new StringBuilder();
		try{
			BufferedReader reader = request.getReader();
			char[]buff = new char[1024];
			int len;
			while((len = reader.read(buff)) != -1) {
				sb.append(buff,0, len);
			}
			param = getParamMap(sb);
		}catch (Exception e) {
			e.printStackTrace();
		}
		logger.info(sb);

		return param;
	}

	private static Map<String,String> getParamMap(StringBuilder sb) throws Exception{
		Map<String,String> map = new HashMap<String,String>();
		String[] str = sb.toString().split("&");
		for(int i=0;i<str.length;i++){
			String[] lit = str[i].split("=");
			if(lit.length==1){
				map.put(URLDecoder.decode(lit[0],"UTF-8"),"");
			}else if(lit.length==2){
				map.put(URLDecoder.decode(lit[0],"UTF-8"),URLDecoder.decode(lit[1],"UTF-8"));
			}
		}
		return map;
	}

	/**
	 * 验证字符串只能是数字，字母或数字加字母组合
	 * @param str
	 * @return
     */
	public static boolean CharacterNumber(String str){
		String reg = "^[0-9a-zA-Z]{6,16}$";
		if(Pattern.matches(reg, str)){
			return true;
		}
		return false;
	}
}